** Descriptive Analyses
cd "/Users/mmoral/Dropbox/--Tamamlanan Çalışma ve Yayınlar/--Elections and Public Opinion in Turkey_Through the Prism of the 2018 Elections/-Replication/Politics as (Un)Usual? An Overview of the June 2018 Presidential and Parliamentary Elections in Turkey/"
use "Replication Data.dta", clear

** Parliamentary Elections (Party Vote Share, Effective Number of Parties, Change in Party Vote Share between consecutive elections) 
foreach elec in 2018P 2015N 2015J{
noisily capture foreach par in akp chp mhp hdp other iyi sp cumhur millet{
gen `par'sh_`elec'=`par'_`elec'/valid_`elec'
gen `par'sq_`elec'=`par'sh_`elec'^2
}
}

egen enep_2018P=rowtotal(akpsq_2018P chpsq_2018P mhpsq_2018P hdpsq_2018P othersq_2018P iyisq_2018P spsq_2018P cumhursq_2018P milletsq_2018P)
replace enep_2018P=1/enep_2018P
foreach elec in 2015N 2015J{
egen enep_`elec'=rowtotal(akpsq_`elec' chpsq_`elec' mhpsq_`elec' hdpsq_`elec' othersq_`elec')
replace enep_`elec'=1/enep_`elec'
}

sum enep_* [aw=valid_2018P]
foreach par in akp chp mhp hdp sp other {
gen `par'shdif_2018P2=(`par'_2018P/valid_2018P)-(`par'_2015N/valid_2015N)
gen `par'shdif_2018P3=(`par'_2018P/valid_2018P)-(`par'_2015J/valid_2015J)
}

** Presidential Elections (Candidate Vote Share, Effective Number of Candidates, Change in Candidate Vote Share between consecutive elections) 
foreach cand in rte emi sd {
gen `cand'sh_2014=`cand'_2014/valid_2014
gen `cand'shsq_2014=`cand'sh_2014^2
}

foreach cand in erdogan ince aksener demirtas karamollaoglu perincek{
gen `cand'sh_2018C=`cand'_2018C/valid_2018C
gen `cand'shsq_2018C=`cand'sh_2018C^2
}

gen erdogan_2014=rte_2014
gen demirtas_2014=sd_2014
foreach cand in erdogan demirtas{
gen `cand'shdif_2018C=(`cand'_2018C/valid_2018C)-(`cand'_2014/valid_2014)
}

egen enec_2018C=rowtotal(*shsq_2018C)
egen enec_2014=rowtotal(*shsq_2014)
replace enec_2018C=1/enec_2018C
replace enec_2014=1/enec_2014
gen enecdif_2018C=enec_2018C-enec_2014

** Maps
** Effective Number of Parties and Candidates (Figure 2)
spmap enep_2018P using "For Maps.dta", id(id) fcolor("37 37 37" "82 82 82" "115 115 115" "150 150 150" "189 189 189" "217 217 217") osize(vvthin ..) ocolor(white ..) ///
subtitle("Effective Number of Parties", box bcolor(gs12) linegap(*2)) legenda(on) legend(pos(6) ring(1) rows(1) size(small)) legtitle(" ") legorder(lohi) legstyle(1) legcount ///
clmethod(custom) clbreaks(1.29 2.27 2.63 2.95 3.25 3.55 5.01) name(enep1, replace) nodraw

spmap enep_2018P using "For Maps.dta", id(id) fcolor(RdYlGn) osize(vvthin ..) ocolor(white ..) ///
subtitle("Effective Number of Parties", linegap(*2)) legenda(on) legend(pos(6) ring(1) rows(1) forcesize size(small)) legtitle(" ") legorder(lohi) legstyle(1) legcount ///
clmethod(custom) clbreaks(1.29 2.27 2.63 2.95 3.25 3.55 5.01) name(enep2, replace) nodraw

spmap enec_2018C using "For Maps.dta", id(id) fcolor("37 37 37" "82 82 82" "115 115 115" "150 150 150" "189 189 189" "217 217 217") osize(vvthin ..) ocolor(white ..) ///
subtitle("Effective Number of Candidates", box bcolor(gs12) linegap(*2)) legenda(on) legend(pos(6) ring(1) rows(1) size(small)) legtitle(" ") legorder(lohi) legstyle(1) legcount ///
clmethod(custom) clbreaks(1.08 1.61 1.87 2.12 2.32 2.54 3.38) name(enec1, replace) nodraw

spmap enec_2018C using "For Maps.dta", id(id) fcolor(RdYlGn) osize(vvthin ..) ocolor(white ..) ///
subtitle("Effective Number of Candidates", linegap(*2)) legenda(on) legend(pos(6) ring(1) rows(1) size(small)) legtitle(" ") legorder(lohi) legstyle(1) legcount ///
clmethod(custom) clbreaks(1.08 1.61 1.87 2.12 2.32 2.54 3.38) name(enec2, replace) nodraw

/*
graph combine enep1 enec1, xcommon cols(1) iscale(*1.6) altshrink
graph export "Figure2_BW.pdf", replace
*/

graph combine enep2 enec2, xcommon cols(1) iscale(*1.6) altshrink
graph export "Figure2_Color.pdf", replace

** In-text explanation
egen gr_enec=cut(enec_2018C), at(1.08 1.61 1.87 2.12 2.32 2.54 3.39)
egen gr_enep=cut(enep_2018P), at(1.29 2.27 2.63 2.95 3.25 3.55 5.01)
local i=1
foreach cut in 1.08 1.61 1.87 2.12 2.32 2.54 3.39{
replace gr_enec=`i' if round(gr_enec,.01)==round(`cut',.01)
local i=`i'+1
}	
local i=1
foreach cut in 1.29 2.27 2.63 2.95 3.25 3.55 5.01{
replace gr_enep=`i' if round(gr_enep,.01)==round(`cut',.01)
local i=`i'+1
}
	
tab gr_enep gr_enec if gr_enep!=gr_enec
gen gr_dif=gr_enep-gr_enec
replace gr_dif=.a if gr_dif<=1 & gr_dif>=-1
tab gr_enep gr_enec if gr_dif!=0
tabstat erdogansh_2018C akpsh_2018P mhpsh_2018P incesh_2018C chpsh_2018P aksenersh_2018C iyish_2018P demirtassh_2018C hdpsh_2018P

** Change and Continuity in Electoral Support for Political Parties (Figure 3)
/*
loc list1 "akpshdif_2018P2 chpshdif_2018P2 mhpshdif_2018P2 hdpshdif_2018P2 akpsh_2018P chpsh_2018P mhpsh_2018P hdpsh_2018P iyish_2018P"
foreach v of varlist `list1' {
egen z_`v'=std(`v')
}

loc list2 "z_akpshdif_2018P2 z_chpshdif_2018P2 z_mhpshdif_2018P2 z_hdpshdif_2018P2 z_akpsh_2018P z_chpsh_2018P z_mhpsh_2018P z_hdpsh_2018P z_iyish_2018P"
forval k = 1(1)36 {
cluster kmeans `list2', k(`k') start(random(12345)) name(cs`k')
}

mat WSS = J(36,5,.)
mat colnames WSS = k WSS log(WSS) eta-squared PRE

forval k = 1(1)36 {
scalar ws`k' = 0
foreach v of varlist `list2' {
quietly anova `v' cs`k'
scalar ws`k' = ws`k' + e(rss)
}
mat WSS[`ḱ', 1] = `k'
mat WSS[`k', 2] = ws`k'
mat WSS[`k', 3] = log(ws`k')
mat WSS[`k', 4] = 1 - ws`k'/WSS[1,2]
mat WSS[`k', 5] = (WSS[`k'-1,2] - ws`k')/WSS[`k'-1,2]
}

mat list WSS

loc squared = char(178)

_matplot WSS, columns(2 1) connect(l) xlabel(#10) name(plot1, replace) nodraw noname
_matplot WSS, columns(3 1) connect(l) xlabel(#10) name(plot2, replace) nodraw noname
_matplot WSS, columns(4 1) connect(l) xlabel(#10) name(plot3, replace) nodraw noname ytitle({&eta}`squared')
_matplot WSS, columns(5 1) connect(l) xlabel(#10) name(plot4, replace) nodraw noname
gr combine plot1 plot2 plot3 plot4, name(plot1to4, replace) 
*/

set seed 12345
cluster kmeans akpshdif_2018P2 chpshdif_2018P2 mhpshdif_2018P2 hdpshdif_2018P2 akpsh_2018P chpsh_2018P mhpsh_2018P hdpsh_2018P iyish_2018P, k(8) gen(gr_parl) iterate(1000000)
tabstat akpsh_2018P akpshdif_2018P2 chpsh_2018P chpshdif_2018P2 mhpsh_2018P mhpshdif_2018P2 hdpsh_2018P hdpshdif_2018P2 iyish_2018P [aw=voter_2018P], by(gr_parl)

recode gr_parl (5=1) (1=3) (6=4) (4=5) (7=6) (8=7) (3=8), gen(gr_map1)
forval i=1/8{
qui sum gr_map1 if gr_map1==`i'
lab def parl_map `i' "Cluster `i' (`r(N)')", modify
}
lab val gr_map1 parl_map

/*
spmap gr_map1 using "For Maps.dta", id(id) fcolor(Greys) osize(vvthin ..) ocolor(white ..) ///
legenda(on) legend(pos(6) ring(1) rows(1) size(small)) /// 
legtitle(" ") legorder(lohi) clmethod(unique) name(change1, replace)
graph export "Figure3_BW.pdf", replace
*/

spmap gr_map1 using "For Maps.dta", id(id) fcolor(Spectral) osize(vvthin ..) ocolor(white ..) ///
legenda(on) legend(pos(6) ring(1) rows(1) forcesize size(small)) /// 
legtitle(" ") legorder(lohi) clmethod(unique)
graph export "Figure3_Color.pdf", replace

** Change and Continuity in Electoral Support for Presidential Candidates (Figure 4)
/*
loc list1 "erdogansh_2018C erdoganshdif_2018C incesh_2018C emish_2014 demirtassh_2018C demirtasshdif_2018C aksenersh_2018C karamollaoglush_2018C"
foreach v of varlist `list1' {
egen z_`v'=std(`v')
}

loc list2 "z_erdogansh_2018C z_erdoganshdif_2018C z_incesh_2018C z_emish_2014 z_demirtassh_2018C z_demirtasshdif_2018C z_aksenersh_2018C z_karamollaoglush_2018C"
forval k = 1(1)28 {
cluster kmeans `list2', k(`k') start(random(12345)) name(cs`k')
}

mat WSS = J(28,5,.)
mat colnames WSS = k WSS log(WSS) eta-squared PRE

forval k = 1(1)28 {
scalar ws`k' = 0
foreach v of varlist `list2' {
quietly anova `v' cs`k'
scalar ws`k' = ws`k' + e(rss)
}
mat WSS[`ḱ', 1] = `k'
mat WSS[`k', 2] = ws`k'
mat WSS[`k', 3] = log(ws`k')
mat WSS[`k', 4] = 1 - ws`k'/WSS[1,2]
mat WSS[`k', 5] = (WSS[`k'-1,2] - ws`k')/WSS[`k'-1,2]
}

mat list WSS

loc squared = char(178)

_matplot WSS, columns(2 1) connect(l) xlabel(#10) name(plot1, replace) nodraw noname
_matplot WSS, columns(3 1) connect(l) xlabel(#10) name(plot2, replace) nodraw noname
_matplot WSS, columns(4 1) connect(l) xlabel(#10) name(plot3, replace) nodraw noname ytitle({&eta}`squared')
_matplot WSS, columns(5 1) connect(l) xlabel(#10) name(plot4, replace) nodraw noname
gr combine plot1 plot2 plot3 plot4, name(plot1to4, replace) 
*/

set seed 12345
cluster kmeans erdogansh_2018C erdoganshdif_2018C incesh_2018C emish_2014 demirtassh_2018C demirtasshdif_2018C aksenersh_2018C karamollaoglush_2018C, k(7) gen(gr_cand) iterate(1000000)
tabstat erdogansh_2018C erdoganshdif_2018C incesh_2018C emish_2014 demirtassh_2018C demirtasshdif_2018C aksenersh_2018C karamollaoglush_2018C [aw=voter_2018C], by(gr_cand)

recode gr_cand (5=1) (1=3) (7=5) (3=7), gen(gr_map2)
forval i=1/7{
qui sum gr_map2 if gr_map2==`i'
lab def cand_map `i' "Cluster `i' (`r(N)')", modify
}
lab val gr_map2 cand_map

/*
spmap gr_map2 using "For Maps.dta", id(id) fcolor(Greys) osize(vvthin ..) ocolor(white ..) ///
legenda(on) legend(pos(6) ring(1) rows(1) size(small)) /// 
legtitle(" ") legorder(lohi) clmethod(unique) name(change3, replace)
graph export "Figure4_BW.pdf", replace
*/

spmap gr_map2 using "For Maps.dta", id(id) fcolor(Spectral) osize(vvthin ..) ocolor(white ..) ///
legenda(on) legend(pos(6) ring(1) rows(1) size(small)) /// 
legtitle(" ") legorder(lohi) clmethod(unique)
graph export "Figure4_Color.pdf", replace
